﻿// -----------------------------------------------------------------------
// <copyright file="Garage.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    public class Garage
    {
        private readonly Dictionary<string, Customer> m_Customers;

        public Garage()
        {
            m_Customers = new Dictionary<string, Customer>();
        }

        public bool AddNewCustomer(Customer i_CustomerToAdd)
        {
            bool successfullyAdded;
            
            if(m_Customers.ContainsKey(i_CustomerToAdd.CustomerVehicle.LicenseNumber))
            {
                UpdateVehicleStatus(i_CustomerToAdd.CustomerVehicle.LicenseNumber, Customer.eVehicleStatus.InRepair);
                successfullyAdded = false;
            }
            else
            {
                m_Customers.Add(i_CustomerToAdd.CustomerVehicle.LicenseNumber, i_CustomerToAdd);
                successfullyAdded = true;
            }

            return successfullyAdded;
        }

        public StringBuilder CreateCustomerLicenseNumberList()
        {
            StringBuilder LicenseNumbersList = new StringBuilder();
            foreach (var currentCustomer in m_Customers)
            {
                LicenseNumbersList.Append(currentCustomer.Value.CustomerVehicle.LicenseNumber + System.Environment.NewLine);
            }

            return LicenseNumbersList;
        }

        /// <summary>
        /// This function enables the user to filter the customers list by the vehicle status in the garage
        /// </summary>
        /// <param name="i_StatusToPrint">the status to filter by</param>
        /// <returns>A StringBuilder of all the vehicles that match the status</returns>
        public StringBuilder CreateCustomerLicenseNumberListByVehicleStatus(Customer.eVehicleStatus i_StatusToPrint)
        {
            StringBuilder LicenseNumbersList = new StringBuilder();
            foreach (var currentCustomer in m_Customers)
            {
                if (i_StatusToPrint == currentCustomer.Value.VehicleStatus)
                {
                    LicenseNumbersList.Append(currentCustomer.Value.CustomerVehicle.LicenseNumber + System.Environment.NewLine);
                }
            }

            return LicenseNumbersList;
        }

        public bool IsVehicleInGarage(string i_LicenseNumber)
        {
            bool found = false;
        
            found = m_Customers.ContainsKey(i_LicenseNumber);

            return found;
        }

        public bool UpdateVehicleStatus(string i_LicenseNumber, Customer.eVehicleStatus i_StatusToUpdate)
        {
            bool found = false;

            found = m_Customers.ContainsKey(i_LicenseNumber);

            if (found)
            {
                m_Customers[i_LicenseNumber].VehicleStatus = i_StatusToUpdate;
            }

            return found;
        }

        /// <summary>
        /// This function enables the user to inflate the selected vehicle's wheels to maximum pressure
        /// </summary>
        /// <param name="i_LicenseNumber">The license number of the selected vehicle</param>
        public void InflateWheelsToMaximum(string i_LicenseNumber)
        {
            if (m_Customers.ContainsKey(i_LicenseNumber))
            {
                m_Customers[i_LicenseNumber].CustomerVehicle.InflateWheelsToMaximum();
            }
            else
            {
                throw new Exception("Vehicle not found, please enter a valid license number");
            }
        }

        public bool RechargeEnergySource(string i_LicenseNumber, EnergySource.eEnergySourceType i_EnergySourceType, float i_AmountToAdd)
        {
            bool found = false;

            found = m_Customers.ContainsKey(i_LicenseNumber);
            
            if(found)
            {
                m_Customers[i_LicenseNumber].CustomerVehicle.RechargeEnergySource(i_EnergySourceType, i_AmountToAdd);
            }

            return found;
        }

        public StringBuilder CreateCustomerDataPrint(string i_LicenseNumber)
        {
            StringBuilder customerDataString = new StringBuilder();
            
            if(m_Customers.ContainsKey(i_LicenseNumber))
            {
                customerDataString.Append(m_Customers[i_LicenseNumber].GetCustomerDataPrint());
            }

            return customerDataString;
        }

        public List<string> ReturnSpecialVehicleFeatures(Vehicle i_vehicleToUpdate)
        {
            return i_vehicleToUpdate.ReturnSpecialFeatures();
        }
    }
}
